#
# Ruby Soul - Ruby Embedded Parsing/Lexing using DSL techniques.
#
# Copyright (c) 2008 - Giancarlo Bellido. All Rights Reserved.
#
#

require 'soul/match'
require 'soul/special'
require 'soul/parser'
require 'soul/char'

module Soul

  Version = 0.1

  #
  # Base Class for Soul Primitives
  # Primitives should only match a single character, including nil or ''
  #
  class Primitive
    
    attr_accessor :match
    
    def initialize(match=nil, &action)
      @match = match
      @action = action
    end
    
    # Parses input
    def include?(input)
      return @match.include?(input)
    end
    
    def parse(input, skipper=Soul::nothing)
      return Soul::Parser.new(self, input, skipper).parse
    end
    
    # Creates a new parser using the new_match class
    def clone_match(new_match)
      return Soul::Primitive.new(new_match.new(self.match))
    end
    
    #
    # Unary Operators
    #
    
    # Matches P or an empty string
    def -@
      return self.clone_match(Soul::ZeroOrOneMatch)      
    end
    
    # Matches P zero or more times
    def !
      return self.clone_match(Soul::ZeroOrMoreMatch)
    end
    
    # Matches P one or more times
    def +@
      return self.clone_match(Soul::OneOrMoreMatch)    
    end
    
    # Matches anything that does not match P
    def ~@
      return self.clone_match(Soul::NotMatch)
    end
    
    #
    # Binary Operators
    #
    
    # Matches self followed by p2
    def >> (p2)
      return Soul::Primitive.new(Soul::SequenceMatch.new(self.match, p2.match))
    end
    
  end
  
end
